package audi.a4.control.core;

import java.util.ArrayList;

public abstract class AbstractAudiA4Packet {
	private double timeStamp;
	private AudiA4CanCode code;
	private int ID;
	private int [] parsedData = null;
	public AbstractAudiA4Packet(double timeStamp,AudiA4CanCode code, int ID,ArrayList<String> data) {
		if(!validate(code))
			throw new RuntimeException("AudiA4CanCode does not match packet type");
		setID(ID);
		this.setTimeStamp(timeStamp);
		this.setCode(code);
		int [] byteData =  new int[data.size()];
		for(int i=0;i<data.size();i++) {
			String s=data.get(i);
			byteData[i] = Integer.parseInt(s, 16);
			
		}
		parse(byteData);
	}
	
	/**
	 * Parse the data for the given opcode
	 * @param data the raw byte data
	 */
	public abstract boolean validate(AudiA4CanCode code);
	
	/**
	 * Parse the data for the given opcode
	 * @param data the raw byte data
	 */
	public abstract void parse(int [] byteData);
	

	public void setTimeStamp(double timeStamp) {
		this.timeStamp = timeStamp;
	}

	public double getTimeStamp() {
		return timeStamp;
	}

	public void setCode(AudiA4CanCode code) {
		this.code = code;
	}

	public AudiA4CanCode getAudiA4CanCode() {
		return code;
	}
	public int getCanCodeNumber() {
		return ID;
	}

	public void setParsedData(int [] parsedData) {
		this.parsedData = parsedData;
	}

	public int [] getParsedData() {
		if (parsedData == null) {
			setParsedData(new int[0]);
		}
		return parsedData;
	}
	
	public String toString() {
		String s="Code = "+AudiA4CanCode.getCodeHex(getID())+" "+code+" time="+timeStamp+" data=[";
		for(int i=0;i<getParsedData().length;i++) {
			s+=" "+getParsedData()[i];
		}
		return s+" ]";
	}

	public void setID(int iD) {
		ID = iD;
	}

	public int getID() {
		return ID;
	}
}
